function r = correlation(signal, template)
%CORRELATION counts cross-correlation between  signal and template
%   INPUT:
%       signal - signal row-vector where template is searching
%       template - template row-vector to search
%   OUTPUT:
%       r - correlation function

% size of correlation array r[] = size of signal array
r = zeros(1, length(signal));
% calculating denominator of correlation coefficient to increase algoritm speed
denominator = sqrt(sumsqr(signal) * sumsqr(template));
% extending signal array to elliminate side-effect
extended_signal = zeros(1, length(signal) + length(template));
extended_signal(1:length(signal)) = signal;
for j=1:length(r)
    r(j) = extended_signal(j:(j + length(template) - 1 )) * template';
end;
 r = r / denominator;
end